package com.canbot.u05.versionupdate;

import android.app.Dialog;
import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

/**
 * 通用提示框.
 */

public class CommonDialog {

    private Context context;

    public CommonDialog(Context context) {
        this.context = context;
    }

    /**
     * 提示对话框 两个button
     *
     * @param title
     * @param hintText
     * @param onSelect
     * @return
     */
    public Dialog hintDialog(String title, String hintText, final OnSelectListener onSelect) {
        //创建对话框对象
        final Dialog tipDialog = new FullScreenDialog(context, R.style.MyDialogStyle);
        View view = LayoutInflater.from(context).inflate(R.layout.dialog_hint_update, null);
        tipDialog.setContentView(view);
        TextView tvensure = (TextView) view.findViewById(R.id.tv_ensure);
        TextView tvcancle = (TextView) view.findViewById(R.id.tv_cancle);
        TextView tvhintText = (TextView) view.findViewById(R.id.tv_hintText);
        TextView tvtitle = (TextView) view.findViewById(R.id.tv_title);
        //初始化数据
        tvtitle.setText(title);
        tvhintText.setText(hintText);

        tipDialog.setCancelable(true);
        tvcancle.setOnClickListener(v -> {
            onSelect.onCancle(v);
            tipDialog.dismiss();
        });
        tvensure.setOnClickListener(v -> {
            onSelect.onEnsure(v);
            tipDialog.dismiss();
        });


        Window window = tipDialog.getWindow();
        window.setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
        tipDialog.show();
        setDialogWindowAttr(window, context.getResources().getDimension(R.dimen.x588),
                context.getResources().getDimension(R.dimen.x340));
        return tipDialog;
    }

    //设置对话框显示
    private void setDialogWindowAttr(Window window, float width, float height) {
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.gravity = Gravity.CENTER;
        lp.width = (int) width;//宽高可设置具体大小
        lp.height = (int) height;
        window.setAttributes(lp);
    }

    public interface OnSelectListener {
        void onCancle(View view);

        void onEnsure(View view);
    }
}
